/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.com.pc9.pswing.components.filebrowser;

import br.com.pc9.pswing.util.SystemUtilsOS;
import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.io.File;
import javax.swing.JComboBox;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

/**
 *
 * @author paulocanedo
 */
public class FileBrowserListenerFactory {

    private FileBrowserModelFactory modelFactory;

    public FileBrowserListenerFactory(FileBrowserModelFactory modelFactory) {
        this.modelFactory = modelFactory;
    }

    public ActionListener createBackListener() {
        return new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                File file = modelFactory.getBrowserListModel().back();
                fireFolderChanged(null, file);
            }
        };
    }

    public ActionListener createForwardListener() {
        return new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                File file = modelFactory.getBrowserListModel().forward();
                fireFolderChanged(null, file);
            }
        };
    }

    public ActionListener createGotoListener() {
        return new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                String input = javax.swing.JOptionPane.showInputDialog(null, "Ir para a pasta:");
                if (input != null && !input.isEmpty()) {
                    if (SystemUtilsOS.isWindows() && input.endsWith(":")) {
                        input += "\\";
                    }

                    File file = new File(input);
                    if (file.exists()) {
                        FileBrowserListModel browserListModel = modelFactory.getBrowserListModel();
                        browserListModel.open(file);
                        fireFolderChanged(null, file);
                    }
                }
            }
        };
    }

    public MouseListener createBrowserMouseListener(final JList source) {
        return new MouseAdapter() {

            @Override
            public void mouseClicked(MouseEvent e) {
                if (SystemUtilsOS.isLeftDoubleClick(e)) {
                    PFileView selectedValue = (PFileView) source.getSelectedValue();

                    fireFolderChanged(source, selectedValue.getFile());
                }
            }
        };
    }

    public ItemListener createNavigationItemListener(final JComboBox source) {
        return new ItemListener() {

            @Override
            public void itemStateChanged(ItemEvent e) {
                PFileView selectedItem = (PFileView) source.getSelectedItem();
                fireFolderChanged(source, selectedItem.getFile());
            }
        };
    }

    public ListSelectionListener createFavoriteNavigationListSelectionListener(final JList source) {
        return new GeneralFileBrowserListener(source);
    }

    public MouseListener createHotfileMouseListener(final JList source) {
        return new GeneralFileBrowserListener(source);
    }

    public ActionListener createNewFolderAction(Component parent) {
        return new NewFolderListener(parent);
    }

    private void fireFolderChanged(Object source, File newFolder) {
        if (!newFolder.isDirectory()) {
            return;
        }

        FileBrowserListModel browserListModel = modelFactory.getBrowserListModel();
        browserListModel.open(newFolder);

        if (!(source instanceof JComboBox)) {
            FileNavigationComboBoxModel navigationComboBoxModel = modelFactory.getNavigationComboBoxModel();
            navigationComboBoxModel.open(newFolder);
        }
    }

    class GeneralFileBrowserListener extends MouseAdapter implements ListSelectionListener, ItemListener {

        private JList source;

        public GeneralFileBrowserListener(JList source) {
            this.source = source;
        }

        public void action() {
            PFileView selectedValue = (PFileView) source.getSelectedValue();
            fireFolderChanged(source, selectedValue.getFile());
        }

        @Override
        public void mouseReleased(MouseEvent e) {
            action();
        }

        @Override
        public void valueChanged(ListSelectionEvent e) {
            action();
        }

        @Override
        public void itemStateChanged(ItemEvent e) {
            action();
        }
    }

    class NewFolderListener implements ActionListener {

        private Component parent;

        public NewFolderListener(Component parent) {
            this.parent = parent;
        }

        @Override
        public void actionPerformed(ActionEvent e) {
            String result = JOptionPane.showInputDialog(parent, "Informe o nome da nova pasta:");
            if (result == null) {
                return;
            }
            
            File currentDir = modelFactory.getBrowserListModel().getCurrentFolder();
            if (currentDir.exists() && currentDir.isDirectory()) {
                File newFolder = new File(currentDir, result);
                if (!newFolder.mkdir()) {
                    JOptionPane.showMessageDialog(parent, "Falha na criação da pasta, sem permissão ou uso de caractere inválido.");
                } else {
                    modelFactory.getBrowserListModel().refresh();
                }
            }
        }
    }
}
